using System;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.Network;
using OracleInternal.TTC.Accessors;

namespace OracleInternal.TTC
{
	internal class TTCDescribe : TTCFunction
	{
		private const byte OPERATIONFLAGS = 7;

		private const long SQLPARSEVERSION = 2L;

		private const int INVALID_CURSOR_ID = 0;

		internal const int DESCRIBE_SQLTEXT = 0;

		private const bool UDSARRAYO2U = true;

		private const bool NUMUDSO2U = true;

		internal TTCDescribe(MarshallingEngine mEngine)
			: base(mEngine, 98, 0)
		{
		}

		private void WriteMessage(int cursorId, byte[] sqltext)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.WriteMessage);
			}
			try
			{
				bool num = cursorId != 0 || sqltext == null || sqltext.Length == 0;
				WriteFunctionHeader();
				m_marshallingEngine.MarshalUB1(7);
				m_marshallingEngine.MarshalSWORD(cursorId);
				if (num)
				{
					m_marshallingEngine.MarshalNullPointer();
					m_marshallingEngine.MarshalSB4(0);
				}
				else
				{
					m_marshallingEngine.MarshalPointer();
					m_marshallingEngine.MarshalSB4(sqltext.Length);
				}
				m_marshallingEngine.MarshalUB4(2L);
				m_marshallingEngine.MarshalO2U(notnull: true);
				m_marshallingEngine.MarshalO2U(notnull: true);
				if (!num)
				{
					m_marshallingEngine.MarshalCHR(sqltext);
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.WriteMessage, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.WriteMessage);
				}
			}
		}

		internal void WriteMessage(int cursor)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.WriteMessage);
			}
			try
			{
				WriteMessage(cursor, null);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.WriteMessage, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.WriteMessage);
				}
			}
		}

		internal void WriteMessage(byte[] sqltext)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.WriteMessage);
			}
			try
			{
				WriteMessage(0, sqltext);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.WriteMessage, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.WriteMessage);
				}
			}
		}

		internal void ReadMessage(Accessor[] accessors)
		{
			int n = m_marshallingEngine.UnmarshalUB1();
			m_marshallingEngine.UnmarshalNBytes_ScanOnly(n);
			m_marshallingEngine.UnmarshalUB4();
		}

		internal void ReadMessageForRefCursor(Accessor[] accessors)
		{
			m_marshallingEngine.UnmarshalUB1();
			m_marshallingEngine.UnmarshalUB4();
		}

		internal void ReadMessage(SQLMetaData sqlMetaData)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.ReadMessage);
			}
			Exception ex = null;
			bool flag = false;
			try
			{
				m_marshallingEngine.TTCErrorObject.Initialize();
				while (!flag)
				{
					try
					{
						switch (m_marshallingEngine.UnmarshalSB1())
						{
						case 23:
							ProcessServerSidePiggybackFunction(0);
							break;
						case 8:
							TTCDescribeInfo.ReadMessage(bForDescribe: true, bForRefCursor: false, m_marshallingEngine, sqlMetaData, bIgnoreMetadata: false);
							break;
						case 15:
							m_marshallingEngine.TTCErrorObject.ReadWarning();
							break;
						case 4:
							m_marshallingEngine.TTCErrorObject.ReadErrorMessage();
							flag = true;
							break;
						case 9:
							if (m_marshallingEngine.HasEOCSCapability)
							{
								m_marshallingEngine.m_endOfCallStatus = m_marshallingEngine.UnmarshalUB4();
							}
							flag = true;
							break;
						default:
							throw new Exception("TTCDescribe:ReadMessage() - Unexpected Packet received.");
						}
					}
					catch (NetworkException ex2)
					{
						if (ex2.ErrorCode != 3111)
						{
							throw;
						}
						m_marshallingEngine.m_oracleCommunication.Reset();
					}
					catch (Exception ex3)
					{
						ex = ex3;
						if (m_marshallingEngine.m_oraBufRdr != null)
						{
							m_marshallingEngine.m_oraBufRdr.ClearState();
						}
						m_marshallingEngine.m_oracleCommunication.Break();
						m_marshallingEngine.m_oracleCommunication.Reset();
					}
				}
				if (ex != null)
				{
					throw ex;
				}
			}
			catch (Exception ex4)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.ReadMessage, ex4);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCDescribe, OracleTraceFuncName.ReadMessage);
				}
			}
		}
	}
}
